package com.itheima.controller;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class PrivilegeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        //判断是否已经登录  session中有没有user
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user==null){
            //没登录 跳转到登录界面
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }
        //已登录 访问目标资源.
        return true;
    }
}
